<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="!dataForm.prizeId ? '新增' : !disabled ? '修改' : '查看'"
    :visible.sync="visible">
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="80px"
             @keyup.enter.native="dataFormSubmit()">
      <el-form-item label="奖品类型" prop="type">
        <el-radio v-model="dataForm.type" :label="1" @change="change">卡券</el-radio>
        <el-radio v-model="dataForm.type" :label="4" @change="change">红包</el-radio>
        <el-radio v-model="dataForm.type" :label="-1" @change="change">谢谢惠顾</el-radio>
      </el-form-item>
      <el-form-item :label="(dataForm.type==1?'卡券':'')+'奖品'" prop="commodityId">
        <el-input v-model="dataForm.prizeName" :disabled="disabled" placeholder="奖品">
          <el-button slot="append" :disabled="disabled" icon="el-icon-search" plain type="success"
                     @click="clickeSelect">选择
          </el-button>
        </el-input>
        商品ID
        <el-input v-model="dataForm.commodityId" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item v-if="dataForm.type !== -1" label="活动" prop="activityName">
        <!--        <el-input v-model="dataForm.commodityId" :disabled="disabled" placeholder="奖品"></el-input>-->
        <el-select
          v-model="dataForm.activityId"
          :disabled="disabled"
          filterable
          placeholder="请选择活动"
          value-key="coneId"
          @change="activityChange">
          <el-option
            v-for="item in activityList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="抽奖分类" prop="categoryId">
        <el-dict v-model="dataForm.categoryId" :clearable="false" code="PRIZE_CATEGORY"></el-dict>
      </el-form-item>
      <el-form-item label="奖品图片" prop="image">
        <el-img v-model="dataForm.image" :disabled="disabled"></el-img>
      </el-form-item>
      <el-form-item label="概率" prop="probability">
        <el-input-number v-model="dataForm.probability" :disabled="disabled" :max="100" :precision="4"
                         :step="0.1"></el-input-number>
      </el-form-item>
      <el-form-item label="角度" prop="angle">
        <el-input-number v-model="dataForm.angle" :disabled="disabled" :max="360" :precision="2"
                         :step="0.1"></el-input-number>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="dataForm.sort" :disabled="disabled" :max="999" :min="1"
                         label="排序"></el-input-number>
      </el-form-item>
      <el-form-item label="奖品库存" prop="number">
        <el-input-number v-model="dataForm.number" :disabled="disabled" :max="999999999" :min="0"
                         label="库存"></el-input-number>
      </el-form-item>
      <el-form-item label="奖品等级" prop="level">
        <el-input-number v-model="dataForm.level" :disabled="disabled" :max="99" :min="1"
                         label="奖品等级"></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>

    <el-drawer
      :modal="false"
      :visible.sync="goodsDrawer"
      :withHeader="false"
      direction="rtl"
      size="60%">
      <div style="padding: 15px;overflow: auto;height: 100vh">
        <goods ref="goodsRef" v-model="goods" @input="valueTransmit"></goods>
      </div>
    </el-drawer>

  </el-dialog>
</template>

<script>
import goods from './goods'

export default {
  data() {
    return {
      disabled: false,
      visible: false,
      goodsDrawer: false,
      dataForm: {
        prizeId: 0,
        prizeName: '',
        image: '',
        addTime: '',
        modifyTime: '',
        userId: '',
        probability: 0.1,
        angle: 0.1,
        sort: '',
        version: '',
        number: '',
        level: 1,
        categoryId: '',
        categoryName: '',
        activityId: '',
        type: 1,
        commodityId: ''
      },
      activityList: [],
      goods: {},
      dataRule: {
        commodityId: [
          {
            validator: (rule, value, callback) => {
              if (this.dataForm.type !== -1 && value === '') {
                callback(new Error('奖品名称不能为空'));
              }
              callback();
            }, trigger: 'blur'
          }
        ],
        activityId: [
          {required: true, message: '请选择活动', trigger: 'blur'}
        ],
        image: [
          {required: true, message: '图片不能为空', trigger: 'blur'}
        ],
        probability: [
          {required: true, message: '中奖率不能为空', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '排序不能为空', trigger: 'blur'}
        ],
        number: [
          {required: true, message: '库存不能为空', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '类型不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    clickeSelect() {
      if (this.dataForm.type === -1) {
        return
      }
      this.goodsDrawer = true;
      this.$nextTick(() => {
        this.$refs.goodsRef.activatedMethod(this.dataForm.type);
      });
    },
    getActivityData() {
      this.$http({
        url: `/mall/activity/queryAll/`,
        method: 'get'
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.activityList = data.list
        }
      })
    },
    activityChange(e) {
      this.dataList.forEach(element => {
        if (element.id == e) {
          this.dataForm.activityId = element.id;
        }
      })
    },
    valueTransmit(v) {
      this.dataForm.commodityId = v.id
      this.dataForm.commodityName = v.name
      this.dataForm.image = v.listPicUrl || v.imgUrl
      this.goodsDrawer = false
      // if(this.dataForm.prizeName.trim() == ''){
      this.dataForm.prizeName = v.name
      // }
    },
    init(id, disabled) {
      this.getActivityData();
      this.disabled = disabled
      this.dataForm.prizeId = id || ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.prizeId) {
          this.$http({
            url: `/mall/prize/info/${this.dataForm.prizeId}`,
            method: 'get'
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm = data.prize
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm']
        .validate((valid) => {
          if (valid) {
            this.$http({
              url: `/mall/prize/${!this.dataForm.prizeId ? 'save' : 'update'}`,
              method: 'post',
              data: this.dataForm
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          }
        })
    },
    change() {
      this.dataForm.commodityId = ''
      this.dataForm.prizeName = ''
    }
  },
  components: {
    goods
  }
}
</script>
